﻿/*Copyright (c) 2012 Cesar Ramirez
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial 
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MechTactics.GameElements;

namespace MechTactics
{
    public class Player
    {
        public int ore { get; set; }
        public int playerNumber { get; set; }
        public bool isActive { get; set; }
        public string playerName { get; set; }
        public TileMap map { get; set; }
        public KeyValuePair<int, int> startingPosition { get; set; }
        public List<GameObject> objectList { get; set; }
        public int score;

        public Player(int playerNumber)
        {
            this.playerNumber = playerNumber;
            this.isActive = true;
            this.score = 0;
        }

        public void setInitialParams(KeyValuePair<int, int> startingPosition, int startingOre, TileMap map)
        {
            this.startingPosition = startingPosition;
            this.ore = startingOre;
            this.map = map;
            this.objectList = new List<GameObject>(0);
        }

        public void InitGame()
        {
            switch (this.playerNumber)
            {
                case 0:
                    this.createBuilding(this.playerNumber * 10, 'A', startingPosition.Key, startingPosition.Value);
                    this.createBuilding(this.playerNumber * 10 + 1, 'B', startingPosition.Key - 1, startingPosition.Value + 1);
                    this.createBuilding(this.playerNumber * 10 + 2, 'C', startingPosition.Key - 1, startingPosition.Value - 1);
                    this.createBuilding(this.playerNumber * 10 + 3, 'D', startingPosition.Key + 1, startingPosition.Value + 1);
                    this.createBuilding(this.playerNumber * 10 + 4, 'E', startingPosition.Key + 1, startingPosition.Value - 1);
                    this.createUnit(this.playerNumber * 10 + 5, 'L', startingPosition.Key + 1, startingPosition.Value);
                    this.createUnit(this.playerNumber * 10 + 6, 'M', startingPosition.Key, startingPosition.Value + 1);
                    break;
                case 1:
                    this.createBuilding(this.playerNumber * 10, 'A', startingPosition.Key, startingPosition.Value);
                    this.createBuilding(this.playerNumber * 10 + 1, 'B', startingPosition.Key + 1, startingPosition.Value - 1);
                    this.createBuilding(this.playerNumber * 10 + 2, 'C', startingPosition.Key + 1, startingPosition.Value + 1);
                    this.createBuilding(this.playerNumber * 10 + 3, 'D', startingPosition.Key - 1, startingPosition.Value - 1);
                    this.createBuilding(this.playerNumber * 10 + 4, 'E', startingPosition.Key - 1, startingPosition.Value + 1);
                    this.createUnit(this.playerNumber * 10 + 5, 'L', startingPosition.Key - 1, startingPosition.Value);
                    this.createUnit(this.playerNumber * 10 + 6, 'M', startingPosition.Key, startingPosition.Value - 1);
                    break;
            }
        }

        public void createUnit(int id, char type, int x, int y)
        {
            Unit unit = new Unit(type, this.playerNumber, id, x, y);
            this.map.add(unit);
            this.objectList.Add(unit);
        }
        public void createBuilding(int id, char type, int x, int y)
        {
            Building building = new Building(type, this.playerNumber, id, x, y);
            this.map.add(building);
            this.objectList.Add(building);
        }

        public void startTurn()
        {
            int numberOfMines = 0;
            foreach (GameObject element in this.objectList)
            {
                element.setActive(true);
                element.setHasMoved(false);
                if (element.checkIfUnit())
                {
                    
                }
                else
                {
                    if (element.getType() == 'B')
                        numberOfMines++;
                }
                
            }
            ore = ore + numberOfMines * Constants.ORE_PER_MINE;
        }

        public void endTurn()
        {
            foreach (GameObject element in this.objectList)
            {
                if (element.checkIfUnit())
                {
                    element.setActive(false);
                }
                element.setWasAttacked(false);
            }
        }

        public GameObject getObject(int id)
        {
            foreach (GameObject element in objectList)
            {
                if (element.getId() == id)
                    return element;
            }
            return null;
        }

        public void remove(int id)
        {
            for (int i = 0; i < objectList.Count; i++)
            {
                GameObject element = objectList.ElementAt(i);
                if (element.getId() == id)
                {
                    this.objectList.Remove(element);
                    this.map.remove(element);
                }
            }
        }

        public void move(int id, int x, int y)
        {
            GameObject element = getObject(id);
            this.map.remove(element);
            element.setLocation(x, y);
            this.map.add(element);
        }

        public int getNumberOfObjects()
        {
            return objectList.Count;
        }

        public void clearElements()
        {
            objectList.Clear();
        }
    }
}
